package com.oba.base.convertor;

import java.io.Serializable;

import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

@SuppressWarnings("unchecked")
public class EnumConvertor implements Converter, Serializable {

	private static final long serialVersionUID = 1L;

	public Object getAsObject(FacesContext context, UIComponent comp,
			String name) {
		ValueExpression ve = comp.getValueExpression("value");
		Class classzz = ve.getType(context.getELContext());
		if (name == null)
			return null;
		if (classzz.isArray())
			classzz = classzz.getComponentType();
		return Enum.valueOf(classzz, name);
	}

	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object enumObj) {
		if (enumObj == null)
			return null;
		else
			return ((Enum) enumObj).name();
	}

}
